using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;

using DataTransferObject;
using Business;


namespace Presentation
{
    public partial class FrmInThe : Form
    {
        clsThuThuBUS ThuThuBUS = new clsThuThuBUS();
        DataTable tbl = new DataTable();
        string duongdanhinh = "";
        int khoa = DateTime.Now.Year;
        string[] st=new string [100];

        public FrmInThe()
        {
            InitializeComponent();
        }

        private void FrmInThe_Load(object sender, EventArgs e)
        {
            tbl = ThuThuBUS .DocGiaBUS .DanhSachDocGia ();
            TaoCot();
            LoadListView(tbl, lvwdocgia);

        }

        private void TaoCot()
        {
            int a = Convert.ToInt16(lvwdocgia.Width / 5);
            lvwdocgia.Columns.Add("Mã Thẻ", a, HorizontalAlignment.Left);
            lvwdocgia.Columns.Add("Họ Tên", a, HorizontalAlignment.Left);
            lvwdocgia.Columns.Add("Ngày Sinh", a, HorizontalAlignment.Center);
            lvwdocgia.Columns.Add("Ngày Lập Thẻ", a, HorizontalAlignment.Center);
            lvwdocgia.Columns.Add("Địa Chỉ", a, HorizontalAlignment.Left);
            lvwdocgia.Columns.Add("Hinh", a, HorizontalAlignment.Left);

        }

        private void LoadListView(DataTable tbl, ListView lvw)
        {
            ListViewItem lvwitem = new ListViewItem();
            lvw.Items.Clear();
            foreach (DataRow dr in tbl.Rows)
            {
                lvwitem = new ListViewItem();
                lvwitem.Text = dr["MaThe"].ToString();

                lvwitem.SubItems.Add(dr["HoTen"].ToString());
                DateTime ngaysinh = Convert.ToDateTime(dr["NgaySinh"].ToString());
                lvwitem.SubItems.Add(ngaysinh.ToShortDateString());
                DateTime ngaylapthe = Convert.ToDateTime(dr["NgayLapThe"].ToString());
                lvwitem.SubItems.Add(ngaylapthe.ToShortDateString());
                lvwitem.SubItems.Add(dr["DiaChi"].ToString());

                string hinh = dr["HinhAnh"].ToString();
                //pchinhdg.Image = System.Drawing.Image.FromFile(Application.StartupPath + @"\" + hinh);
                lvwitem.SubItems.Add(hinh);
                lvw.Items.Add(lvwitem);
            }

        }

        private void lvwdocgia_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwdocgia.SelectedItems.Count == 0)
                return;
            lblsothe.Text = lvwdocgia.SelectedItems[0].SubItems[0].Text;
            lblhoten.Text = lvwdocgia.SelectedItems[0].SubItems[1].Text;
            lblngaysinh.Text = lvwdocgia.SelectedItems[0].SubItems[2].Text;
            lblngaylapthe.Text = lvwdocgia.SelectedItems[0].SubItems[3].Text;
            //int khoa = DateTime.Now.Year;
            lblkhoa.Text = Convert.ToString(khoa.ToString());
            duongdanhinh = lvwdocgia.SelectedItems[0].SubItems[5].Text;

            String url = Application.StartupPath + @"\" + duongdanhinh;
            if (System.IO.File.Exists(url))
            {
                pchinhdg.Image = System.Drawing.Image.FromFile(url);
            }
            else
                pchinhdg.Image = null;
            //pchinhdg.Image = System.Drawing.Image.FromFile(Application.StartupPath + @"\" + duongdanhinh);
        }

        private void btndanhdautatca_Click(object sender, EventArgs e)
        {
            if (btndanhdautatca.Text == "Đánh dấu tất cả")
            {
                btndanhdautatca.Text = "Hủy đánh dấu";
                for (int i = 0; i < lvwdocgia.Items.Count; i++)
                    lvwdocgia.Items[i].Checked = true;
            }
            else
            {
                if (btndanhdautatca.Text == "Hủy đánh dấu")
                {
                    btndanhdautatca.Text = "Đánh dấu tất cả";
                    for (int i = 0; i < lvwdocgia.Items.Count; i++)
                        lvwdocgia.Items[i].Checked = false;
                }
               
            }
           
        }

      

        int j = 0, i = 0; int tam1 = 0;
       

      

    

        private void pchinhdg_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog2.ShowDialog() == DialogResult.OK)
                {
                    pchinhdg.BackgroundImage = Image.FromFile(openFileDialog2.FileName);
                    st[lvwdocgia.SelectedItems[0].Index] = openFileDialog2.FileName;
                }
            }
            catch { }
        }

        private void btninthe_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printdm;
            printDialog1.AllowSomePages = true;
            DialogResult dlr = new DialogResult();
            dlr = printDialog1.ShowDialog();
            if (dlr == System.Windows.Forms.DialogResult.Yes)
            {
                printdm.Print();
            }
        }

        private void btnxemthu_Click(object sender, EventArgs e)
        {
            printdl.ShowDialog();
        }

        private void printdm_PrintPage(object sender, PrintPageEventArgs e)
        {
            while (j < lvwdocgia.CheckedItems.Count)
            {
                if (tam1 == 3)
                {
                    e.HasMorePages = true;
                    tam1 = 0;
                    i = 0;
                    return;
                }
                else
                {

                    tam1++;
                    //  e.Graphics.DrawString (Pens.Blue, groupPanel1.Location.X, groupPanel1.Location.Y + i);
                    //  e.Graphics.DrawString(groupPanel1.Text, groupPanel1.Font , Brushes.Black, groupPanel1.Location.X, groupPanel1.Location.Y + i);
                    // e.Graphics.DrawArc(groupPanel1.TitleImage , "", "", "", "", "", "");
                    e.Graphics.DrawString(labelX1.Text, labelX1.Font, Brushes.Black, labelX1.Location.X, labelX1.Location.Y + i);
                    e.Graphics.DrawString(labelX2.Text, labelX2.Font, Brushes.Black, labelX2.Location.X, labelX2.Location.Y + i);
                    e.Graphics.DrawString(labelX3.Text, labelX3.Font, Brushes.Black, labelX3.Location.X, labelX3.Location.Y + i);
                    e.Graphics.DrawString(labelX4.Text, labelX4.Font, Brushes.Black, labelX4.Location.X, labelX4.Location.Y + i);
                    // e.Graphics.DrawString(txt5.Text, txt5.Font, Brushes.Black, txt5.Location.X, txt5.Location.Y + i);
                    e.Graphics.DrawString(lvwdocgia.CheckedItems[j].Text, lblsothe.Font, Brushes.Black, lblsothe.Location.X, lblsothe.Location.Y + i);
                    e.Graphics.DrawString(labelX7.Text, labelX7.Font, Brushes.Black, labelX7.Location.X, labelX7.Location.Y + i);
                    e.Graphics.DrawString(lvwdocgia.CheckedItems[j].SubItems[1].Text, lblhoten.Font, Brushes.Black, lblhoten.Location.X, lblhoten.Location.Y + i);
                    e.Graphics.DrawString(labelX8.Text, labelX8.Font, Brushes.Black, labelX8.Location.X, labelX8.Location.Y + i);
                    e.Graphics.DrawString(lvwdocgia.CheckedItems[j].SubItems[2].Text, lblngaysinh.Font, Brushes.Black, lblngaysinh.Location.X, lblngaysinh.Location.Y + i);
                    e.Graphics.DrawString(labelX9.Text, labelX9.Font, Brushes.Black, labelX9.Location.X, labelX9.Location.Y + i);
                    e.Graphics.DrawString(lvwdocgia.CheckedItems[j].SubItems[3].Text, lblngaylapthe.Font, Brushes.Black, lblngaylapthe.Location.X, lblngaylapthe.Location.Y + i);
                    e.Graphics.DrawString(labelX10.Text, labelX10.Font, Brushes.Black, labelX10.Location.X, labelX10.Location.Y + i);
                    e.Graphics.DrawString(khoa.ToString(), lblkhoa.Font, Brushes.Black, lblkhoa.Location.X, lblkhoa.Location.Y + i);

                    e.Graphics.DrawImage(pclogo.Image, pclogo.Location.X, pclogo.Location.Y + i, pclogo.Width, pclogo.Height);

                    try
                    {

                        e.Graphics.DrawImage(Image.FromFile(Application.StartupPath + @"\" + lvwdocgia.CheckedItems[j].SubItems[5].Text),
                                             new Rectangle(pchinhdg.Location.X, pchinhdg.Location.Y + i,
                                             pchinhdg.Width, pchinhdg.Height));


                        
                    }

                    catch { };
                    i = i + labelX3.Location.Y + 300;
                    j++;
                }
            }
            // e.HasMorePages = false;
        }

     


    }
}